package com.zx._01_基础._019_servlet;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录信息处理
 *
 * @author 张雪
 */
public class CookieServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        // 模拟处理结果是正确的，就开始如下的代码

        // 1.创建cookies
        Cookie newCookie = new Cookie("login_information", name + "0.0" + password);
        // 2.设置cookies的有效时间（2*60秒）
        newCookie.setMaxAge(2 * 60);
        // 3.将生成的cookies返回的客户端
        response.addCookie(newCookie);
        request.getRequestDispatcher("true.jsp").forward(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
